home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_4_6.zip / ECHO.C < prev    next >
C/C++ Source or Header  |  1991-02-21  |  9KB  |  287 lines

  1. /****************************************************************************
  2. Module name: Echo.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCOLOR
  8. #undef NOCTLMGR
  9. #undef NOHELP
  10. #undef NOKERNEL
  11. #undef NOMB
  12. #undef NOMEMMGR
  13. #undef NOMENUS
  14. #undef NOMSG
  15. #undef NOSHOWWINDOW
  16. #undef NOUSER
  17. #undef NOVIRTUALKEYCODES
  18. #undef NOWH
  19. #undef NOWINMESSAGES
  20. #undef NOWINSTYLES
  21. #include <windows.h>
  22.  
  23. #include "Echo.h"
  24. #include "Recorder.h"
  25.  
  26.  
  27. //****************************************************************************
  28. char _szAppName[] = "Echo";
  29.  
  30. HANDLE _hInstance = NULL;  // Our instance handle.
  31. HWND _hWndApp = NULL;      // Main application's window handle.
  32.  
  33. LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam);
  34. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam);
  35. DWORD FAR PASCAL MsgFilterHookFunc (int nCode, WORD wParam, LPMSG lpMsg);
  36.  
  37. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
  38.    WNDCLASS WndClass;
  39.    MSG msg;
  40.  
  41.    _hInstance = hInstance;
  42.  
  43.    WndClass.style         = 0;
  44.    WndClass.lpfnWndProc   = AppWndProc;
  45.    WndClass.cbClsExtra    = 0;
  46.    WndClass.cbWndExtra    = 0;
  47.    WndClass.hInstance     = hInstance;
  48.    WndClass.hIcon         = LoadIcon(hInstance, _szAppName);
  49.    WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  50.    WndClass.hbrBackground = COLOR_WINDOW + 1;
  51.    WndClass.lpszMenuName  = _szAppName;
  52.    WndClass.lpszClassName = _szAppName;
  53.    RegisterClass(&WndClass);
  54.  
  55.    // Create application window, store in global variable.
  56.    _hWndApp = CreateWindow(_szAppName, _szAppName, WS_OVERLAPPEDWINDOW,
  57.       CW_USEDEFAULT, SW_SHOW, CW_USEDEFAULT, CW_USEDEFAULT,
  58.       NULL, NULL, hInstance, 0);
  59.  
  60.    if (_hWndApp == NULL) return(0);
  61.    ShowWindow(_hWndApp, nCmdShow);
  62.    UpdateWindow(_hWndApp);
  63.  
  64.    while (GetMessage(&msg, NULL, 0, 0)) {
  65.       TranslateMessage(&msg);
  66.       DispatchMessage(&msg);
  67.    }
  68.    return(0);
  69. }
  70.  
  71.  
  72. // Window defined message sent by the Recorder function in the RECORDER.C 
  73. // DLL when recording or playing of events is stopped.
  74. #define USER_RECORDER      (WM_USER + 0)  
  75.  
  76. LONG FAR PASCAL AppWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam) {
  77.    static BOOL fRecording = FALSE, fPlaying = FALSE;
  78.    static GLOBALHANDLE hMacro;   // Global handle containing recorded events.
  79.    BOOL fCallDefProc = FALSE;
  80.    LONG lResult = 0;
  81.    FARPROC fpMsgFilter, fpDlgProc;
  82.    RECRESULT RecResult = REC_OK;
  83.    char *szRecMsg = NULL;
  84.  
  85.    switch (wMsg) {
  86.  
  87.       case WM_DESTROY:
  88.          // Close the Windows Help Engine.
  89.          WinHelp(hWnd, NULL, HELP_QUIT, NULL);
  90.          PostQuitMessage(0);
  91.          break;
  92.  
  93.       case USER_RECORDER:
  94.          // Message sent when recording or playing is stopped.
  95.          if (wParam == 0) {   // Playing stopped.
  96.             fPlaying = FALSE;
  97.             if ((RECRESULT) lParam == REC_SYSMODALON)
  98.                MessageBox(hWnd, "System Modal Dialog Box - Playing Halted",
  99.                   _szAppName, MB_SYSTEMMODAL | MB_ICONHAND | MB_OK);
  100.             break;
  101.          }
  102.  
  103.          // Recording stopped.
  104.          // wParam = GLOBALHANDLE of block, lParam = RECRESULT.
  105.          fRecording = FALSE;
  106.          hMacro = wParam;
  107.  
  108.          if ((RECRESULT) lParam == REC_TOOMANY)
  109.             MessageBox(NULL, "Out of memory", _szAppName,
  110.                MB_SYSTEMMODAL | MB_ICONHAND | MB_OK);
  111.  
  112.          if ((RECRESULT) lParam == REC_SYSMODALON)
  113.             MessageBox(hWnd, "System Modal Dialog Box - Recording Halted",
  114.                _szAppName, MB_SYSTEMMODAL | MB_ICONHAND | MB_OK);
  115.          break;
  116.  
  117.       case WM_INITMENU:
  118.          // User is working with the menu, enable/disable options.
  119.          EnableMenuItem(GetMenu(hWnd), IDM_STARTRECORD,
  120.             MF_BYCOMMAND |
  121.             ((fRecording || fPlaying) ? MF_GRAYED : MF_ENABLED));
  122.          EnableMenuItem(GetMenu(hWnd), IDM_STOPRECORD,
  123.             MF_BYCOMMAND | (fRecording ? MF_ENABLED : MF_GRAYED));
  124.          EnableMenuItem(GetMenu(hWnd), IDM_STARTPLAYBACK,
  125.             MF_BYCOMMAND |
  126.             ((fRecording || fPlaying || hMacro == NULL)
  127.             ? MF_GRAYED : MF_ENABLED));
  128.          break;
  129.  
  130.       case WM_COMMAND:
  131.          switch (wParam) {
  132.  
  133.          case IDM_EXIT:
  134.             SendMessage(hWnd, WM_CLOSE, 0, 0);
  135.             break;
  136.  
  137.          case IDM_ABOUT:
  138.             // Create procedural-instance for task-specific filter function.
  139.             fpMsgFilter =
  140.                MakeProcInstance((FARPROC) MsgFilterHookFunc, _hInstance);
  141.             SetWindowsHook(WH_MSGFILTER, fpMsgFilter);
  142.  
  143.             fpDlgProc = MakeProcInstance(AboutProc, _hInstance);
  144.             DialogBox(_hInstance, "About", hWnd, fpDlgProc);
  145.             FreeProcInstance(fpDlgProc);
  146.  
  147.             // Remove the filter function from the chain.
  148.             UnhookWindowsHook(WH_MSGFILTER, fpMsgFilter);
  149.             FreeProcInstance(fpMsgFilter);
  150.  
  151.             break;
  152.  
  153.          case IDM_STARTRECORD:
  154.             // If a macro was already recorded, free it.
  155.             if (hMacro != NULL) GlobalFree(hMacro);
  156.  
  157.             fRecording = TRUE;
  158.  
  159.             // Last parameter is handle to this window and message that
  160.             // should be sent when recording is stopped.
  161.             RecResult = Recorder(RM_STARTRECORD, 0, MAKELONG(hWnd, USER_RECORDER));
  162.             break;
  163.  
  164.          case IDM_STOPRECORD:
  165.             RecResult = Recorder(RM_STOPRECORD, 0, 0);
  166.             break;
  167.  
  168.          case IDM_STARTPLAYBACK:
  169.             fPlaying = TRUE;
  170.  
  171.             // Last parameter is handle to this window and message that
  172.             // should be sent when playing is stopped.
  173.             RecResult = Recorder(RM_STARTPLAY, hMacro, MAKELONG(hWnd, USER_RECORDER));
  174.             break;
  175.  
  176.          default:
  177.             break;
  178.          }
  179.  
  180.          // Inform user if an error occurred with the recorder.
  181.          switch (RecResult) {
  182.             case REC_ACTIVE:
  183.                szRecMsg = "Recorder already recording/playing.";
  184.                break;
  185.  
  186.             case REC_INACTIVE:
  187.                szRecMsg = "Recorder already stopped.";
  188.                break;
  189.  
  190.             case REC_NOMEMORY:
  191.                szRecMsg = "Insufficient memory to start recording.";
  192.                break;
  193.  
  194.             case REC_NOEVENTS:
  195.                szRecMsg = "No events to playback.";
  196.                break;
  197.          }
  198.  
  199.          if (szRecMsg != NULL)
  200.             MessageBox(hWnd, szRecMsg, _szAppName,
  201.                MB_OK | MB_ICONINFORMATION);
  202.  
  203.          break;
  204.  
  205.       default:
  206.          fCallDefProc = TRUE; break;
  207.    }
  208.  
  209.    if (fCallDefProc)
  210.       lResult = DefWindowProc(hWnd, wMsg, wParam, lParam);
  211.  
  212.    return(lResult);
  213. }
  214.  
  215.  
  216. // ***************************************************************************
  217.  
  218. static FARPROC _fnNextMsgFilterHookFunc = NULL;
  219.  
  220. DWORD FAR PASCAL MsgFilterHookFunc (int nCode, WORD wParam, LPMSG lpMsg) {
  221.    BOOL fCallDefProc = FALSE;
  222.    DWORD dwResult = 0;
  223.  
  224.    switch (nCode) {
  225.  
  226.       case MSGF_DIALOGBOX:
  227.          // Message is for the About dialog box because we know that
  228.          // this is the only dialog box created by this application.
  229.  
  230.          if (lpMsg->message != WM_KEYDOWN || lpMsg->wParam != VK_F1) {
  231.             fCallDefProc = TRUE;
  232.             break;
  233.          }
  234.  
  235.          // Message is WM_KEYDOWN and key is the F1 key, display help.
  236.          WinHelp(_hWndApp, NULL, HELP_HELPONHELP, NULL);
  237.          dwResult = 1;  // Tell Windows we processed the message.
  238.          break;
  239.  
  240.       case MSGF_MENU:
  241.       case MSGF_SCROLLBAR:
  242.       case MSGF_NEXTWINDOW:
  243.       default:
  244.          fCallDefProc = TRUE;
  245.          break;
  246.    }
  247.  
  248.    if ((nCode < 0) || (fCallDefProc && (_fnNextMsgFilterHookFunc != NULL)))
  249.       dwResult = DefHookProc(nCode, wParam, (LONG) lpMsg,
  250.                      &_fnNextMsgFilterHookFunc);
  251.  
  252.    return (dwResult);
  253. }
  254.  
  255. // ***************************************************************************
  256. // This function processess all messages sent to the About dialog box.
  257.  
  258. BOOL FAR PASCAL AboutProc (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam) {
  259.    BOOL fProcessed = TRUE;
  260.    char szBuffer[100];
  261.  
  262.    switch (wMsg) {
  263.  
  264.       case WM_INITDIALOG:
  265.          // Set version static window to have date and time of compilation.
  266.          wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  267.          SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  268.          break;
  269.  
  270.       case WM_COMMAND:
  271.          switch (wParam) {
  272.             case IDOK: case IDCANCEL:
  273.                if (HIWORD(lParam) == BN_CLICKED)
  274.                   EndDialog(hDlg, wParam);
  275.                break;
  276.  
  277.             default:
  278.                break;
  279.          }
  280.          break;
  281.  
  282.       default:
  283.          fProcessed = FALSE; break;
  284.    }
  285.    return(fProcessed);
  286. }
  287.